package com.gao.weblog.web.consumer;

import com.gao.weblog.common.domain.mapper.ArticleMapper;
import com.gao.weblog.common.domain.mapper.StatisticsArticlePVMapper;
import com.gao.weblog.web.constant.RabbitConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;


@Component
@Slf4j
public class ReadArticleConsumer {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private StatisticsArticlePVMapper articlePVMapper;

    @RabbitListener(queues = RabbitConst.READ_NUM_QUEUE)
    public void increaseReadNum(Long articleId){
        log.info("==> 文章阅读事件消费成功，articleId:{}", articleId);

        articleMapper.increaseReadNum(articleId);
        log.info("==> 文章阅读量 +1 操作成功，articleId:{}", articleId);

        LocalDate currDate = LocalDate.now();
        articlePVMapper.increasePVCount(currDate);
        log.info("==> 当日文章 PV 访问量 +1 操作成功，date: {}", currDate);
    }
}
